package cn.xo68.ide.plugin.common.job;

import cn.xo68.ide.plugin.common.service.CodeGenerateManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.vfs.VirtualFile;

public class LoadDatabaseMetaDataJob implements ToolSuiteJob {
    private final String title;
    private JobCallback jobCallback;

    private final VirtualFile projectVirtualFile;


    public LoadDatabaseMetaDataJob(String title, VirtualFile projectVirtualFile) {
        this.title = title;
        this.projectVirtualFile = projectVirtualFile;
        jobCallback=new JobCallbackAdapter(title){
            @Override
            public void success() {
                super.success();
                ApplicationManager.getApplication().invokeLater(()->{
                    LoadDatabaseMetaDataJob.this.projectVirtualFile.refresh(false,false);
                });
            }
        };
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public boolean isModal() {
        return true;
    }

    @Override
    public boolean isStartInBackground() {
        return true;
    }

    @Override
    public JobCallback getCallback() {
        return jobCallback;
    }

    @Override
    public void execute(ProgressIndicator indicator) throws Exception {
        CodeGenerateManager.getInstance().loadRemoteDatabaseMetaData(this.projectVirtualFile);
    }
}
